﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCSLibrary;
using System.Data;
using Utils.DC.Web.UI;

public partial class Admin_Module_dang_ky_nguoi_dung : System.Web.UI.UserControl
{
    #region Cac bien dung chung
    UsersUI objUser = new UsersUI();

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        try
        {
            int result = objUser.Add(txtUserName.Text.Trim(), txtEmail.Text.Trim(), txtFirstName.Text.Trim(), txtLastName.Text.Trim(), (DateTime)txtBirthday.SelectedDate,
                Convert.ToByte(cboSex.SelectedValue), txtMobile.Text.Trim(), txtAddress.Text.Trim(), DateTime.Now, Security.Encrypt(txtPassword.Text.Trim()),
                false, "", DateTime.Now, txtCompanyName.Text, txtCompanyAddress.Text, txtFax.Text, txtPosition.Text, txtJob.Text, "", Convert.ToByte(Utils.Common.USER_TYPE.NotSet));
            if (result > 0)
            {
                lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
            }
            else
            {
                lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        ReInsertData();
    }

    private void LoadEditData(int ID)
    {
        DataSet ds = objUser.GetByID(ID);
        if (ds != null && ds.Tables.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtUserID.Value = row["UserID"].ToString();
            txtUserName.Text = row["UserName"].ToString().Trim();
            txtEmail.Text = row["Email"].ToString().Trim();
            txtPassword.Text = row["Password"].ToString().Trim();
            txtPosition.Text = row["Position"].ToString().Trim();
            txtAddress.Text = row["Position"].ToString().Trim();
            txtBirthday.SelectedDate = (DateTime?)Convert.ToDateTime(row["Birthday"]) ;
            txtCompanyAddress.Text = row["CompanyAddress"].ToString().Trim();
            txtCompanyName.Text = row["CompanyName"].ToString().Trim();
            txtFax.Text = row["Fax"].ToString().Trim();
            txtFirstName.Text = row["FirstName"].ToString().Trim();
            txtJob.Text = row["Job"].ToString().Trim();
            txtLastName.Text = row["LastName"].ToString().Trim();
            txtMobile.Text = row["Mobile"].ToString().Trim();
            cboSex.SelectedValue = row["Sex"].ToString().Trim();
        }
    }

    private void ReInsertData()
    {
        txtUserID.Value = "";
        txtUserName.Text = "";
        txtEmail.Text = "";
        txtPassword.Text = "";
        txtPosition.Text = "";
        txtAddress.Text = "";
        txtBirthday.SelectedDate = (DateTime?)DateTime.Now;
        txtCompanyAddress.Text = "";
        txtCompanyName.Text = "";
        txtFax.Text = "";
        txtFirstName.Text = "";
        txtJob.Text = "";
        txtLastName.Text = "";
        txtMobile.Text = "";
        cboSex.SelectedValue = "";
    }

}
